package com.hifar.test.oaApi.common;

import java.io.OutputStream;
import java.nio.charset.Charset;

import com.alibaba.fastjson.JSON;

/**
 * <pre> 将响应对象流化成JSON。 {@link ObjectMapper}是线程安全的。 </pre>
 */
public class JsonMarshaller implements ApiMarshaller {

	@Override
    public void marshaller(Object object, OutputStream outputStream) {
		try {
			JSON.writeJSONString(outputStream, Charset.forName("utf-8"), object);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public <T> T unmarshaller(String str, Class<T> apiResponse) {
		try {
			return JSON.parseObject(str, apiResponse);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
